package com.xrj4j.k_tally.model

import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Update
import com.xrj4j.k_tally.R
import com.xrj4j.k_tally.utils.DateUtil
import kotlinx.coroutines.flow.Flow
import java.time.LocalDateTime

@Entity(tableName = "tb_bill")
data class BillItem(
    @PrimaryKey(autoGenerate = true) var id: Int?,
    @ColumnInfo("kind") var kind: Int = Kind.OUTCOME.code,
    @ColumnInfo("typeName") var typeName: String = "其他",
    @ColumnInfo("typeIcon") var typeIcon: Int = getDefaultIcon(kind),
    @ColumnInfo("money") var money: Float = 0f,
    @ColumnInfo("remark") var remark: String = "",
    @ColumnInfo("createTime") var createTime: String = DateUtil.formatDate(LocalDateTime.now()),
    @ColumnInfo("year") var year: Int = DateUtil.year(LocalDateTime.now()),
    @ColumnInfo("month") var month: Int = DateUtil.month(LocalDateTime.now()),
    @ColumnInfo("day") var day: Int = DateUtil.day(LocalDateTime.now())
)

fun getDefaultIcon(kind: Int): Int {
    return if (kind == Kind.OUTCOME.code) R.mipmap.ic_qita_fs else R.mipmap.ic_qita
}


@Dao
interface BillItemDao {
    @Query("select * from tb_bill")
    fun loadBillList(): Flow<List<BillItem>>

    @Insert
    fun insert(vararg items: BillItem)

    @Update
    fun update(vararg items: BillItem)

    @Delete
    fun delete(vararg items: BillItem)

}
